window.onload = function() {
    // If you set ?Debug=true in the URL, such as a different App ID
    // in the developer console, include debugging information.
    cast.player.api.setLoggerLevel(cast.player.api.LoggerLevel.DEBUG);

    var mediaElement = document.getElementById('video');
    console.log("Got media element: " + mediaElement);

    // Create the media manager. This will handle all media messages
    // by default.
    window.mediaManager = new cast.receiver.MediaManager(mediaElement);


    // Remember the default value for the Receiver onLoad, so this
    // sample can Play non-adaptive media as well.
    mediaManager.defaultOnLoad = mediaManager.onLoad;
    mediaManager.onLoad = function (event) {
	// The Media Player Library requires that you call player
	// unload between different invocations.
	if (window.player !== null) {
	    player.unload();    // Must unload before starting again.
	    window.player = null;
	}
	// This trivial parser is by no means best practice, it shows
	// how to access event data, and uses the a string search of
	// the suffix, rather than looking at the MIME type which
	// would be better.  In practice, you will know what content
	// you are serving while writing your player.
	if (event.data['media'] && event.data['media']['contentId']) {
	    console.log('Starting media application');
	    var url = event.data['media']['contentId'];
	    // Create the Host - much of your interaction with the
	    // library uses the Host and methods you provide to it.
	    window.host = new cast.player.api.Host(
		{'mediaElement':mediaElement, 'url':url});
	    var ext = url.substring(url.lastIndexOf('.'), url.length);
	    var initStart = event.data['media']['currentTime'] || 0;
	    var autoplay = event.data['autoplay'] || true;
	    var protocol = null;
	    mediaElement.autoplay = autoplay;  // Make sure autoplay get's set
	    if (url.lastIndexOf('.m3u8') >= 0) {
		// HTTP Live Streaming
		protocol = cast.player.api.CreateHlsStreamingProtocol(host);
	    } else if (url.lastIndexOf('.mpd') >= 0) {
		// MPEG-DASH
		protocol = cast.player.api.CreateDashStreamingProtocol(host);
	    } else if (url.indexOf('.ism/') >= 0) {
		// Smooth Streaming
		protocol = cast.player.api.CreateSmoothStreamingProtocol(host);
	    }
	    // How to override a method in Host. I know that it's safe
	    // to just provide this method.
	    host.onError = function(errorCode) {
		console.log("Fatal Error - "+errorCode);
		if (window.player) {
		    window.player.unload();
		    window.player = null;
		}
	    };
	    // If you need cookies, then set withCredentials = true
	    // also set any header information you need.  If you don't
	    // need them, there can be some unexpected effects by
	    // setting this value.
	    //
	    //  host.updateSegmentRequestInfo = function(requestInfo) {
	    //     requestInfo.withCredentials = true;
	    //  };
	    console.log("we have protocol "+ext);
	    if (protocol !== null) {
		console.log("Starting Media Player Library");
		window.player = new cast.player.api.Player(host);
		window.player.load(protocol, initStart);
	    }
	    else {
		mediaManager.defaultOnLoad(event);    // do the default process
	    }
	}
    }
    window.player = null;
    // castReceiverManager.start();
    console.log('Application is ready, starting system');

    var ev = { "data": {
        "media": { "contentId":
		   // "http://localhost:8080/dr1.mp4",
		   // "http://dr01-lh.akamaihd.net/i/dr01_0@147054/index_1700_av-b.m3u8?sd=10&b=100-2000&rebase=on&rebase=on&cors",
		   // "rtsp://lm.gss.dr.dk/V/V04L.stream",
		   // "rtmp://livetv.gss.dr.dk/live/livedr04astream3",
		   // "http://dr04-lh.akamaihd.net/z/dr04_0@147057/manifest.f4m?b=300-1600&rebase=on",
		   "http://lm.gss.dr.dk/V/V04H.stream/Playlist.m3u8",
		   // "http://localhost:8889/?n=livetv.gss.dr.dk\&c=1935\&a=live\&y=livedr06astream1",
		   // "http://dr03-lh.akamaihd.net/i/dr03_0@147056/master.m3u8?b=100-1600&rebase=on",
                   "currentTime": 0 },
        "autoplay": true }};
    console.log("Sending event: " + JSON.stringify(ev));
    window.mediaManager.onLoad(ev);
};
